from config import *
from help import *
from gui_sub import *
from keyr import *
from decimal import Decimal

ic_show = 1
ic_wait=2
ic_force = 3
ic_noshow = 4
ic_wen = 5
ic_bian = 6
#'□'   '△'    '▽'   '〇'
sym_show = '△'
sym_noshow =  '▽'
sym_wen = '♡'
sym_wait = '〇'

#画提示窗口和托盘
class my3(QWidget):
    def __init__(self):
        super().__init__()                                            
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.ToolTip)
        self.setGeometry (ini_pos[0], ini_pos[1], main_win_size[0], main_win_size[1])
        self.l = QLabel(self)
        self.setIni()
        h = QHBoxLayout(self)
        self.l.setStyleSheet("QLabel {border: %spx solid rgb(0, 0, 0); background-color : white;  }" % main_win_border);        
        h.addWidget(self.l)
        d = 0
        h.setContentsMargins(d, d, d, d)
        self.setLayout(h)
        self.co = 0
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(20)
        t2 = QTimer(self)
        t2.timeout.connect(self.ont2)
        t2.start(700)
        self.t2co=0
        self.t2on=False
        self.real_ic = ic_bian

        self.last = None
        self.offset = None
        self.p = True
        self.t = ''        
        self.last_has = False
        self.ic_mode = 0
        self.ic_m = {}
        self.last_j = False
        self.ic_m[ic_force] = QIcon(icon_file_path_force)
        self.ic_m[ic_bian] = QIcon(icon_file_path_bian)
        self.ic_m[ic_wait] = QIcon(icon_file_path_wait)
        self.ic_m[ic_show] = QIcon(icon_file_path_show)
        self.ic_m[ic_noshow] = QIcon(icon_file_path_noshow)
        self.ic_m[ic_wen] = QIcon(icon_file_path_wen)

        self.tray = QSystemTrayIcon(self)
        self.tray.activated.connect(self.trayA)
        self.tray.setToolTip('mm')
        self.setIc(ic_noshow)        
        self.tray.show()

    def setIc(self,a):
        self.real_ic = a
        if self.t2on:
            return
        self.setIcImp(a)        
        
    def setIcImp(self,a):
        if a==self.ic_mode:
            return
        self.ic_mode =a 
        self.tray.setIcon(self.ic_m[a])
    def isXCancel(self):
        if cfg.ctrl2_job and not cfg.ctrl2_other and time.time()-cfg.ctrl2_time>x_cancel_gap:
            return True
        
        if not cfg.middle_pressed and not cfg.x2_pressed:
            return False
        if time.time()-cfg.x_press_time<  x_cancel_gap:
            return False
        if cfg.x2_pressed and cfg.x2_pressed_then_other:
            return False
        if cfg.middle_pressed_other and cfg.middle_pressed:
            return False
        return True
    
    def trayA(self,a): 
        if a == self.tray.Trigger:            
            cfg.pause_key = not cfg.pause_key
            
        if a == self.tray.Context:
            sys.exit(0)
            
    def closeEvent(self, a0):
        print('close')        
        sys.exit(0)
        
    def setIni(self):
        self.resize(main_win_size[0], main_win_size[1]) 
        self.show()
        
    def mouseMoveEvent(self, event):
        if self.offset is not None:
            self.move(self.pos() - self.offset + event.pos())
            
    def mousePressEvent(self, event):
        a =  event.button()
        print(a)
        if a == Qt.RightButton:
            self.hide()
        if a == Qt.MiddleButton:            
            sys.exit(0)
        if a == Qt.LeftButton:                    
            self.offset = event.pos()
   
    def mouseReleaseEvent(self, event):
        a =  event.button()
        if a == Qt.LeftButton:
            self.offset = None
    def get_showLock(self):
        if cfg.lock or (pm.ctrl_pressed and pm.ctrl_other):
            return True        
        return False     
        
    def get_move_icon(self):
        if cfg.isShow:
            ss = '□'
        elif cfg.show_one_on:
            ss = sym_show
            if not cfg.w2showing:
                ss = sym_wait
        else:
            ss = sym_noshow
            
        return ss
    
    def get_l_arr(self,a):
        s = '↙↓↘←→↖↑↗'
        if a<5:
            return s[a-1]
        return s[a-2]
    
    def get_arr_st1(self,dx,dy,moving):
        if moving:
            dx2 = dy2 = 'x'
        elif not cfg.show_jiao:
            dx2 = str(round(abs(dx)))
            dy2 = str(round(abs(dy)))
        else:
            dx2 = str( round(abs(math.atan2(dy,dx)*180/math.pi )))+'°'
            dy2 = str(round(math.sqrt(dx*dx+dy*dy)))

        arr = '%s[%s,%s]' %(get_arr(dx,dy),'{:>2s}'.format(dx2),'{:>2s}'.format(dy2))
        return arr
    
    def get_arr_st2(self,dx,dy):        
        dx2 = str(round(abs(dx)))
        dy2 = str(round(abs(dy)))   
        if Decimal(cfg.last_sum) % 1 == 0:
            xx = round(cfg.last_sum)
        else:
            xx = '%.1f'%cfg.last_sum

        arr = '%s%s(%s,%s)' %(get_arr(dx,dy),xx,'{:>1s}'.format(dx2),'{:>1s}'.format(dy2))
        return arr
    def get_arr_and_last(self,dx,dy,m):
        last_str = self.get_arr_st2(cfg.last_m[0],cfg.last_m[1])
        arr = self.get_arr_st1(dx,dy,m)
        if cfg.last_m[0]==0 and cfg.last_m[1]==0:
            last_str = ''
        if cfg.show_last_m:
            return arr,last_str
        
        if dx==0 and dy==0:
            if not last_str:
                return arr,''
            if cfg.last_m_time>0:
                return '',last_str        
        return arr,''

    def get_text(self):
        if cfg.cmd_mode:
            return 'C',''
        if cfg.set_mode:
            if cfg.set_mode_min:
                return "S- (%s,%s)"%get_cur_pos(),''
            return "S (%s,%s)"%get_cur_pos(),''
        x, y = getp(None,True)
        sx, sy = cfg.fix_pos
        dx = x - sx
        dy = y - sy
        showLock = self.get_showLock()
        plus = ''
        if showLock and cfg.lock_xing_mode==1:
            plus = '*'
        if showLock and cfg.lock_xing_mode==2:
            plus = '+'
        if showLock and cfg.xing_chu:
            plus += '/'
        sum_str = '{:2s}'.format(cfg.sum)
        if time.time()-cfg.left_mouse_time>show_arr_time and time.time()-cfg.right_mouse_time>show_arr_time:
            m  =False
        else:
            m = True
        if jump_state_sup2():
            m = False
        ss = self.get_move_icon()
        arr,last_str = self.get_arr_and_last(dx,dy,m)        
       
        if is_sca():
            sca_str = '%s↻ '%sca_l[cfg.sca % len(sca_l)]                
        else:
            sca_str = ''
        
        if len(cfg.enter_pressed_seq)>=2 and (pm.add or pm.enter):
            start_du,sca = enter_du()
            sca = sca/math.pi*180
            si = cfg.enter_pressed_seq[0]
            if si==5:
                si = cfg.enter_pressed_seq[1]
            ll = ' %s%.1f°'%(self.get_l_arr(si),sca)
        elif showLock:
            ll = '↹(%s%s)'%(self.get_l_arr(cfg.lock_xing_last_move),lock_get())
        else:
            ll = last_str
        
        st = plus+sum_str+arr +sca_str+ll                

        if cfg.force_show:
            st += 'F'
        noshow = cfg.min_ge_force_not_show and not cfg.min_ge_force_show
        if  cfg.min_ge!=0 and (not cfg.min_ge_has_refresh or noshow):                    
            if noshow:
                st += '-'+str(cfg.min_ge)
            else:
                st += str(cfg.min_ge)
            
        if cfg.is_pressed:
            st += '╩'
               
        if pm.scroll:
            if cfg.pause_mode:
                st += ''
            else:
                st += '|'   

        if cfg.alt_mode==2:
            st += '|'   

        if cfg.scroll_one!=0 and not is_sca():
            if cfg.scroll_one%2==1:
                st +='①'
            else:
                st +='②'
        if len(cfg.add_show2) >0:
            st +='&%s'%str(list(cfg.add_show2))
        if cfg.capsShow:
            st += 'C'
        if not cfg.offMid:
            st += '中'
        if self.isXCancel():
            st += '长'
        if cfg.x2_pressed and cfg.has_update_cancle and cfg.x2_type==0:
            st += '双'
        if (cfg.x2_mode and cfg.x2_ini_mouse==1) or (not cfg.x2_mode and cfg.no_left_move):
             st += '左'
        if cfg.g_is_acc:
            if cfg.can_set_acc and cfg.acc_no_reset:
                st +='A'
            elif cfg.can_set_acc:
                st +='U'
            else:
                st +='a'
        if cfg.x2_mode:
            if cfg.x2_mode_slow:
                st += '慢'            
        if cfg.jump_state==0 and not cfg.x2_mode and cfg.jump_kuai in (1,3) :
            st += '快'
        if cfg.jump_state==0 and not cfg.x2_mode and cfg.jump_kuai==2 :
            st += '快2'
        
        if cfg.jump_state==1 and not cfg.x2_mode:
            xxqu = cfg.keep_qu and cfg.keep_qu_dx<jump_qu_gap and time.time()-cfg.right_mouse_time<jump_qu_interval            
            if (cfg.jump_from_click or not cfg.can_break) and not xxqu:
                st += '慢'             
            else:
                st += '趋'    
              
        if cfg.acc_speed == g_mouse_speed[2]:
            st += '按'
            if cfg.ori_kuai!=1:
                st += '2'
        if not cfg.left_jump and is_qu():
            st += '跳'
        if cfg.last_move_is_right_solo:
            st += '右'
        if cfg.twh_has_wheel:
            if (cfg.twh_has_left and cfg.twh_has_right ) or (not cfg.twh_has_right and not cfg.twh_has_left):
                st += '圈'
        if cfg.twh_has_turn!=0:
            if cfg.twh_has_turn<0:
                st += '↻'
            else:
                st += '⭯'
        return st,ss
    
    def ont2(self):                 
        self.t2co += 1
        if (cfg.acc_add>liang_conf[0] or cfg.tt_co>=right_jump_thre) and self.t2co%2==0:
            self.t2on = True
            self.setIcImp(ic_bian)
            self.renderTxt(True)
        else:
            self.t2on = False
            self.setIcImp(self.real_ic)
            self.renderTxt()
        
    def ont(self):                 
        if cfg.to_exit:
            sys.exit(0)

        if cfg.last_active_time!=0 and keep_alive_time>0 and time.time()-cfg.last_active_time>keep_alive_time:
            print('exit')            
            sys.exit(0)

        cur = get_cur_pos()
        mmx = time.time()-cfg.right_mouse_time<jump_qu_interval   
        mm = (time.time()-cfg.left_mouse_time>show_arr_time and time.time()-cfg.right_mouse_time>show_arr_time)
        pmm = pm.get_mode()
        sign =  (cfg.acc_speed,cfg.show_jiao, tuple(cfg.enter_pressed_seq), pmm,cfg.min_ge,cfg.isShow, cfg.xing_chu,cfg.x2_mode_slow,
                 cfg.show_last_m,cfg.show_state, cfg.min_ge_force_not_show,cfg.min_ge_force_show,cfg.jump_state,
                  cfg.fix_pos, cur, cfg.sum,cfg.sca, cfg.sca_du, cfg.sca_cur, cfg.last_m,cfg.last_sum,cfg.last_move_is_right_solo,
                  cfg.left_jump,cfg.jump_from_click,cfg.can_break,mmx,cfg.acc_no_reset,cfg.keep_qu,cfg.keep_qu_dx,
                 cfg.sca_cur_pos, cfg.lock,pm.ctrl_other,tuple(cfg.add_show2),cfg.g_is_acc,cfg.can_set_acc,
                  cfg.lock_step, cfg.pause_key,mm,cfg.alt_mode,cfg.scroll_one,cfg.no_left_move,cfg.x2_ini_mouse,
                 cfg.pause_move, cfg.is_pressed, cfg.move_to_win, cfg.cmd_mode, cfg.set_mode, cfg.set_mode_min,
                   cfg.show_one_on, cfg.always_show_one, cfg.force_show, cfg.w2showing,cfg.jump_kuai,cfg.ori_kuai,
                  cfg.lock_xing_mode, cfg.lock_xing_last_move, cfg.x2_mode,cfg.x2_pressed,cfg.has_update_cancle,
                  cfg.pause_mode,cfg.capsShow,cfg.offMid,self.isXCancel(),cfg.twh_has_left,cfg.twh_has_right,
                  cfg.twh_has_wheel,cfg.twh_has_turn)
        
        if cfg.w3con>=resize_cycle :
                if not self.isHidden():
                    self.setIni()
                cfg.w3con = 0

        if self.last == sign:
            cfg.w3con+=1
            if cfg.w3con>=resize_cycle:
                if not self.isHidden():
                    self.setIni()
                cfg.w3con = 0
            return
        
        if cfg.move_to_win:
            cfg.move_to_win = False
            geo = self.geometry()
            clear(False)
            cfg.fix_pos =  (geo.x(), geo.y())
            con_set_pos(geo.x(), geo.y())
            
        if self.p != cfg.pause_key:
            print('pause key')
            self.p = cfg.pause_key
            if self.isHidden():
                self.setIni()
            else:
                self.hide()
                        
        if cfg.pause_move:
            self.move(round(cfg.pause_move[0]), round(cfg.pause_move[1]))
            self.setIni()
            cfg.pause_move = False
            
        self.last = sign
        st,ss = self.get_text()                              

        if not is_no_move() and sym_wait in ss:
            ss = ss.replace(sym_wait,sym_wen)
        
        if 'F' in st and sym_wait in ss:
            self.setIc(ic_force)
        elif sym_wen in ss:
            self.setIc(ic_wen)
        elif sym_wait in ss:
            self.setIc(ic_wait)
        elif sym_noshow in ss:
            if not cfg.acc_no_reset:
                self.setIc(ic_wen)
            else:
                self.setIc(ic_noshow)
        else:
            self.setIc(ic_show)        
        
        if cfg.show_state:
            st += ss
            
        if self.t == st:
            return        
        self.t = st
        if self.t2on:
            return
        self.renderTxt()
                
    def renderTxt(self,add=False):
        st = self.t
        if add:
            st += str(cfg.tt_co)
        self.l.setText(st)
        f = QFont("Times", main_win_txt_size);
        f.setBold(True)
        self.l.setFont(f);
        ll = ('(' in st or '↻' in st)
        if ll != self.last_has:
            self.last_has = ll
            self.last = (0,)
            cfg.w3con = resize_cycle

# 画x2 mode提示点        
class my4(QMainWindow):
    def __init__(self):
        super().__init__()  
        self.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint|Qt.ToolTip)                
        self.setStyleSheet("background-color:black;")                    
        self.hide()
        self.l = round(32*reso_sca)
        self.setGeometry(200, 200,self.l, self.l)                         
        icon_g = QIcon(dst_arr)     
        self.mouse_cursor_pixmap = icon_g.pixmap(icon_g.actualSize(QSize(self.l,self.l)))
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(100//6)
        self.si = []
        
    def ont(self):
        si = (cfg.x2_mode, cfg.x2_mode_dst[0], cfg.x2_mode_dst[1],cfg.x2_mode_exit_time,
              time.time()-cfg.x2_mode_exit_time<x2_delay)
        if self.si == si:
            return
        self.si = si
        if cfg.x2_mode or time.time()-cfg.x2_mode_exit_time<x2_delay:            
            self.move(round(cfg.x2_mode_dst[0]), round(cfg.x2_mode_dst[1]))
            self.show()            

        else:
            self.hide()            
        
    def paintEvent(self,e):                 
        p = QPainter(self)         
        ma = self.mouse_cursor_pixmap.mask()                   
        self.setMask(ma)                             
        p.setRenderHint(QPainter.Antialiasing);        
        p.drawPixmap(0,0,self.l,self.l,self.mouse_cursor_pixmap)
if __name__ == '__main__':
    app = QApplication([])   
    w = my4()
        
    app.exec_()     